package net.starwifi.duobao.web;

import net.starwifi.duobao.FileInfo;
import net.starwifi.duobao.entity.DBProduct;
import net.starwifi.duobao.entity.DBProductImage;
import net.starwifi.duobao.entity.DBProductType;
import net.starwifi.duobao.entity.DBTimes;
import net.starwifi.duobao.mvc.*;
import net.starwifi.duobao.service.DBProductImageService;
import net.starwifi.duobao.service.DBProductService;
import net.starwifi.duobao.service.DBProductTypeService;
import net.starwifi.duobao.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2015/11/19.
 */
@Controller("adminDBProductController")
@RequestMapping("/dbProduct")
public class DBProductController extends net.starwifi.duobao.mvc.BaseController{

    @Autowired
    DBProductService dbProductService;

    @Autowired
    FileService fileService;

    @Autowired
    DBProductImageService dbProductImageService;

    @Autowired
    DBProductTypeService dbProductTypeService;

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model) {
        model.addAttribute("dbProductTypeList",dbProductTypeService.getListData());
        return "/dbProduct/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(DBProduct entity,Long dbProductTypeId, RedirectAttributes redirectAttributes) {

        entity.setDbProductType(dbProductTypeService.find(dbProductTypeId));
        for (Iterator<DBProductImage> iterator = entity.getProductImages().iterator(); iterator.hasNext();) {
            DBProductImage productImage = iterator.next();
            if (productImage == null || productImage.isEmpty()) {
                iterator.remove();
                continue;
            }
            if (productImage.getFile() != null && !productImage.getFile().isEmpty()) {
                if (!fileService.isValid(FileInfo.FileType.image, productImage.getFile())) {
                    addFlashMessage(redirectAttributes, Message.error("文件类型错误"));
                    return "redirect:add";
                }
            }
        }
        if (!isValid(entity)) {
            return ERROR_VIEW;
        }
        for (DBProductImage productImage : entity.getProductImages()) {
            dbProductImageService.build(productImage,"/db_product_"+entity.getSn()+"/");
        }
        entity.setIsMarketable(Boolean.FALSE);
        dbProductService.save(entity);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        model.addAttribute("entity", dbProductService.find(id));
        model.addAttribute("dbProductTypeList",dbProductTypeService.getListData());
        return "/dbProduct/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(DBProduct entity,Long dbProductTypeId, RedirectAttributes redirectAttributes) {
        entity.setDbProductType( dbProductTypeService.find(dbProductTypeId));
        for (Iterator<DBProductImage> iterator = entity.getProductImages().iterator(); iterator.hasNext();) {
            DBProductImage productImage = iterator.next();
            if (productImage == null || productImage.isEmpty()) {
                iterator.remove();
                continue;
            }
            if (productImage.getFile() != null && !productImage.getFile().isEmpty()) {
                if (!fileService.isValid(FileInfo.FileType.image, productImage.getFile())) {
                    addFlashMessage(redirectAttributes, Message.error("文件类型错误"));
                    return "redirect:add";
                }
            }
        }
        if (!isValid(entity)) {
            return ERROR_VIEW;
        }
        for (DBProductImage productImage : entity.getProductImages()) {
            dbProductImageService.build(productImage,"/db_product_"+entity.getSn()+"/");
        }
        dbProductService.update(entity, "dbTimes","isMarketable");
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(PageView pageView, ModelMap model,HttpServletRequest request) {
        pageView.filterQuery(request);
        pageView.addOrder(Order.desc("id"));
        model.addAttribute("page", dbProductService.getScrollData(pageView));
        return "/dbProduct/list";
    }



    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Message delete(Long[] ids) {
        dbProductService.delete(ids);
        return SUCCESS_MESSAGE;
    }

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping(value = "/jsonList", method = RequestMethod.GET)
    public Object list(Long productTypeId) {
        List<Order> orderBy=new ArrayList<Order>();
        orderBy.add(Order.desc("id"));
        final List<DBProduct> list=dbProductService.getListData("o.dbProductType.id=?1",new Object[]{productTypeId},orderBy);
        return Message.createMessage(Message.success("success"), new HashMap<String, Object>() {{
            put("list", list);
        }});
    }
}
